// 社会评价指标考评
var OrgDemocracy = Ext.data.Record.create([{
	name : 'id',
	mapping : 'id',
	type : 'int'
}, {
	name : 'goalId',
	mapping : 'goalId',
	type : 'int'
}, {
	name : 'orgId',
	mapping : 'orgId',
	type : 'int'
},{
	name : 'score',
	mapping : 'score',
	type : 'float'
},{
	name : 'memo',
	mapping : 'memo',
	type : 'string'
},{
	name : 'checkOrgId',
	mapping : 'checkOrgId',
	type : 'int'
},{
	name : 'year',
	mapping : 'year',
	type : 'int'
},{
	name : 'checkManId',
	mapping : 'checkManId',
	type : 'int'
},{
	name : 'auditOrgId',
	mapping : 'auditOrgId',
	type : 'int'
},{
	name : 'scoreId',
	mapping : 'scoreId',
	type : 'int'
}]);

var sm = new Ext.grid.RowNumberer();
var cm_orgdemocracy = new Ext.grid.ColumnModel([
	new Ext.grid.RowNumberer({header : '序号', width : 40, dataIndex: 'id'}),
	{id: 'goalId',header: "社会评价指标", width: 100, sortable: true, dataIndex: 'goalId',
		renderer : function(v) {
				var ret = '';
				var size = ds_democracygoal_select.getCount();
				for (var i = 0; i < size; i++) {
					var record = ds_democracygoal_select.getAt(i);
					var id = record.get('id');
					if (id == v) {
						ret = record.get('content');
						break;
					}
				}
				return (ret == '' || ret == null) ? v : ret;
			}},
	{id: 'orgId',header: "被考评部门", width: 100, sortable: true, dataIndex: 'orgId',
		renderer : function(v) {
				var ret = '';
				var size = ds_org_select.getCount();
				for (var i = 0; i < size; i++) {
					var record = ds_org_select.getAt(i);
					var id = record.get('id');
					if (id == v) {
						ret = record.get('name');
						break;
					}
				}
				return (ret == '' || ret == null) ? v : ret;
			}},
	{id: 'score',header: "指标项得分", width: 40, sortable: true, dataIndex: 'score'},
	{id: 'memo',header: "备注", width: 120, sortable: true, dataIndex: 'memo'},
	{id: 'checkOrgId',header: "考核责任单位", width: 100, sortable: true, dataIndex: 'checkOrgId',
		renderer : function(v) {
				var ret = '';
				var size = ds_org_select.getCount();
				for (var i = 0; i < size; i++) {
					var record = ds_org_select.getAt(i);
					var id = record.get('id');
					if (id == v) {
						ret = record.get('name');
						break;
					}
				}
				return (ret == '' || ret == null) ? v : ret;
			}},
    {id: 'year',header: "考核年度", width: 100, dataIndex: 'year'},
	{id: 'checkManId',header: "社会评价责任人类型", width: 120, sortable: true, dataIndex: 'checkManId',
		renderer : function(v) {
				var ret = '';
				var size = ds_democracycheckman_select.getCount();
				for (var i = 0; i < size; i++) {
					var record = ds_democracycheckman_select.getAt(i);
					var id = record.get('id');
					if (id == v) {
						ret = record.get('name');
						break;
					}
				}
				return (ret == '' || ret == null) ? v : ret;
			}},
    {id: 'scoreId',header: "评分类型分值", width: 80, sortable: true, dataIndex: 'scoreId',
    	renderer : function(v) {
				var ret = '';
				var size = ds_democracyscore_select.getCount();
				for (var i = 0; i < size; i++) {
					var record = ds_democracyscore_select.getAt(i);
					var id = record.get('id');
					if (id == v) {
						ret = record.get('symbol');
						break;
					}
				}
				return (ret == '' || ret == null) ? v : ret;
			}}
]);

cm_orgdemocracy.defaultSortable = false;
var window_add_orgdemocracy = new Ext.Window({
			title : '添加社会评价指标考评',
			width : 360,
			height : 440,
			resizable : false,
			autoHeight : true,
			modal : true,
			closeAction : 'hide',
			listeners : {
				'hide' : function() {
					this.setTitle('添加社会评价指标考评');
					Ext.getCmp("form_add_orgdemocracy").getForm().reset();
				}
			},
			items : [new Ext.FormPanel({
						id : 'form_add_orgdemocracy',
						labelWidth : 70,
						labelAlign : 'right',
						url : '/supervise/biz/orgdemocracy/saveOrgDemocracy.action',
						method : 'post',
						border : false,
						baseCls : 'x-plain',
						bodyStyle : 'padding:5px 5px 0',
						anchor : '100%',
						defaults : {
							width : 240,
							msgTarget : 'side' // 验证信息显示右边
						},
						defaultType : 'textfield',
						items : [{
								xtype : 'hidden',
								name : 'orgDemocracy.id'
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>社会评价指标',
								name : 'orgDemocracy_goalId',
								hiddenName : 'orgDemocracy.goalId',
								allowBlank : false,
								readOnly : true,
								editable : false,
								store : ds_democracygoal_select,
								displayField : 'content',
								valueField : 'id',
								mode : 'remote',
								triggerAction : 'all',
								emptyText : '请选择社会评价指标',
								listeners : {
									'select' : function(combo, record, index) { 
//										combo.setValue(record.data.id);
									}
								}
							
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>被考核部门',
								hiddenName : 'orgDemocracy.orgId',
								valueField : 'id',
								displayField : 'name',
								mode : 'remote',
								store : ds_org_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择部门',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_add_orgdemocracy").getForm();
										frm.findField('orgDemocracy.orgId').setValue(record.data.id);
									}
								}
							}, {
								xtype : 'numberfield',
								fieldLabel : '<span style="color: red">*&nbsp;</span>社会评价指标项得分',
								name : 'orgDemocracy.score',
								allowBlank : false,
								maxLength : 20
							},{
								xtype: "textarea",
								fieldLabel : '备注',
								name : 'orgDemocracy.memo',
								maxLength : 200
							}, { 
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>考核责任单位',
								hiddenName : 'orgDemocracy.checkOrgId',
								valueField : 'id',
								displayField : 'name',
								mode : 'remote',
								store : ds_org_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择部门',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_add_orgdemocracy").getForm();
										frm.findField('orgDemocracy.checkOrgId').setValue(record.data.id);
									}
								}
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>考核年度',
								hiddenName : 'orgDemocracy.year',
								mode : 'local',
								store : store_year,
								valueField : 'value',
								displayField : 'text',
								value: check_year,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								readOnly : true,
								triggerAction : 'all',
								emptyText : '请选择考核年度'
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>社会评价责任人类型',
								hiddenName : 'orgDemocracy.checkManId',
								valueField : 'id',
								displayField : 'name',
								mode : 'remote',
								store : ds_democracycheckman_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择部门',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_add_orgdemocracy").getForm();
										frm.findField('orgDemocracy.checkManId').setValue(record.data.id);
									}
								}
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>评分类型分值',
								hiddenName : 'orgDemocracy.scoreId',
								valueField : 'id',
								displayField : 'symbol',
								mode : 'remote',
								store : ds_democracyscore_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择评分类型分值',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_add_orgdemocracy").getForm();
										frm.findField('orgDemocracy.scoreId').setValue(record.data.id);
									}
								}
							}],
						buttonAlign : 'center',
						minButtonWidth : 60,
						buttons : [{
									text : '添加',
									handler : function(btn) {
										var frm = Ext.getCmp("form_add_orgdemocracy").getForm();
										if (frm.isValid()) {
											btn.disable();
											var cnfield = frm.findField('orgDemocracy.goalId');
											frm.submit({
														waitTitle : '请稍候',
														waitMsg : '正在提交表单数据,请稍候...',
														success : function(form, action) {
															var store = grid_orgdemocracy.getStore();
															var orgdemocracy = new OrgDemocracy({
																		id : action.result.id,
//																		goalId : form.findField('orgDemocracy.goalId').getValue(),
																		goalId : cnfield.getValue(),
																		orgId : form.findField('orgDemocracy.orgId').getValue(),
																		score : form.findField('orgDemocracy.score').getValue(),
																		memo : form.findField('orgDemocracy.memo').getValue(),
																		checkOrgId : form.findField('orgDemocracy.checkOrgId').getValue(),
																		year : form.findField('orgDemocracy.year').getValue(),
																		checkManId : form.findField('orgDemocracy.checkManId').getValue(),
																		scoreId : form.findField('orgDemocracy.scoreId').getValue()
																	});
															store.insert(0, [orgdemocracy]);
															btn.enable();
															form.reset();
															window_add_orgdemocracy.hide();
														},
														failure : function() {
															Ext.Msg.show({
																		title : '错误提示',
																		msg : '"' + cnfield.getValue() + '" ' + '名称可能已经存在!',
																		buttons : Ext.Msg.OK,
																		fn : function() {
																			cnfield.focus(true);
																			btn.enable();
																		},
																		icon : Ext.Msg.ERROR
																	});
														}
													});
										}
									}
								}, {
									text : '重置',
									handler : function() {
										Ext.getCmp("form_add_orgdemocracy").getForm().reset();
									}
								}, {
									text : '取消',
									handler : function() {
										window_add_orgdemocracy.hide();
									}
								}]
					})]
		});
		
var window_edit_orgdemocracy = new Ext.Window({
			title : '修改社会评价指标考评',
			width : 360,
			height : 440,
			resizable : false,
			autoHeight : true,
			modal : true,
			closeAction : 'hide',
			listeners : {
				'hide' : function() {
					this.setTitle('修改社会评价指标考评');
					Ext.getCmp("form_edit_orgdemocracy").getForm().reset();
				}
			},
			items : [new Ext.FormPanel({
						id : 'form_edit_orgdemocracy',
						labelWidth : 70,
						labelAlign : 'right',
						url : '/supervise/biz/orgdemocracy/saveOrgDemocracy.action',
						method : 'post',
						border : false,
						baseCls : 'x-plain',
						bodyStyle : 'padding:5px 5px 0',
						anchor : '100%',
						defaults : {
							width : 240,
							msgTarget : 'side' // 验证信息显示右边
						},
						defaultType : 'textfield',
						items : [{
								xtype : 'hidden',
								name : 'orgDemocracy.id'
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>社会评价指标',
								name : 'orgDemocracy_goalId',
								hiddenName : 'orgDemocracy.goalId',
								allowBlank : false,
								readOnly : true,
								editable : false,
								store : ds_democracygoal_select,
								displayField : 'content',
								valueField : 'id',
								mode : 'remote',
								triggerAction : 'all',
								emptyText : '请选择社会评价指标',
								listeners : {
									'select' : function(combo, record, index) { 
//										combo.setValue(record.data.id);
									}
								}
							
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>被考核部门',
								hiddenName : 'orgDemocracy.orgId',
								valueField : 'id',
								displayField : 'name',
								mode : 'remote',
								store : ds_org_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择部门',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_edit_orgdemocracy").getForm();
										frm.findField('orgDemocracy.orgId').setValue(record.data.id);
									}
								}
							}, {
								xtype : 'numberfield',
								fieldLabel : '<span style="color: red">*&nbsp;</span>社会评价指标项得分',
								name : 'orgDemocracy.score',
								allowBlank : false,
								maxLength : 20
							},{
								xtype: "textarea",
								fieldLabel : '备注',
								name : 'orgDemocracy.memo',
								maxLength : 200
							}, { 
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>考核责任单位',
								hiddenName : 'orgDemocracy.checkOrgId',
								valueField : 'id',
								displayField : 'name',
								mode : 'remote',
								store : ds_org_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择部门',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_edit_orgdemocracy").getForm();
										frm.findField('orgDemocracy.checkOrgId').setValue(record.data.id);
									}
								}
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>考核年度',
								hiddenName : 'orgDemocracy.year',
								mode : 'local',
								store : store_year,
								valueField : 'value',
								displayField : 'text',
								value: check_year,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								readOnly : true,
								triggerAction : 'all',
								emptyText : '请选择考核年度'
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>社会评价责任人类型',
								hiddenName : 'orgDemocracy.checkManId',
								valueField : 'id',
								displayField : 'name',
								mode : 'remote',
								store : ds_democracycheckman_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择部门',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_edit_orgdemocracy").getForm();
										frm.findField('orgDemocracy.checkManId').setValue(record.data.id);
									}
								}
							}, {
								xtype : 'combo',
								fieldLabel : '<span style="color: red">*&nbsp;</span>评分类型分值',
								hiddenName : 'orgDemocracy.scoreId',
								valueField : 'id',
								displayField : 'symbol',
								mode : 'remote',
								store : ds_democracyscore_select,
								selectOnFocus : true,
								editable : false,
								allowBlank : false,
								triggerAction : 'all',
								loadingText : '加载中...',
								emptyText : '请选择评分类型分值',
								listeners : {
									'select' : function(combo, record, index) {
										var frm = Ext.getCmp("form_edit_orgdemocracy").getForm();
										frm.findField('orgDemocracy.scoreId').setValue(record.data.id);
									}
								}
							}],
						buttonAlign : 'center',
						minButtonWidth : 60,
						buttons : [{
									text : '修改',
									handler : function(btn) {
										var frm = Ext.getCmp("form_edit_orgdemocracy").getForm();
										if (frm.isValid()) {
											btn.disable();
											var cnfield = frm.findField('orgDemocracy.content');
											frm.submit({
														waitTitle : '请稍候',
														waitMsg : '正在提交表单数据,请稍候...',
														success : function(form, action) {
															var record = grid_orgdemocracy.getSelectionModel().getSelected();
															// set value
															record.set("orgId", frm.findField('orgDemocracy.orgId').getValue());
															record.set("score", frm.findField('orgDemocracy.score').getValue());
															record.set("memo", frm.findField('orgDemocracy.memo').getValue());
															record.set("checkOrgId", frm.findField('orgDemocracy.checkOrgId').getValue());
															record.set("year", frm.findField('orgDemocracy.year').getValue());
															record.set("checkManId", frm.findField('orgDemocracy.checkManId').getValue());
															record.set("scoreId", frm.findField('orgDemocracy.scoreId').getValue());
															frm.reset();
															btn.enable();
															window_edit_orgdemocracy.hide();
														},
														failure : function() {
															Ext.Msg.show({
																		title : '错误提示',
																		msg : '"' + cnfield.getValue() + '" ' + '名称可能已经存在!',
																		buttons : Ext.Msg.OK,
																		fn : function() {
																			cnfield.focus(true);
																			btn.enable();
																		},
																		icon : Ext.Msg.ERROR
																	});
														}
													});
										}
									}
								}, {
									text : '取消',
									handler : function() {
										window_edit_orgdemocracy.hide();
									}
								}]
					})]
		});
var btn_add_orgdemocracy = new Ext.Button({
			text : '添加',
			iconCls : 'icon-add',
			handler : function() {
				window_add_orgdemocracy.show(this);
				window_add_orgdemocracy.center();
			}
		});
var btn_edit_orgdemocracy = new Ext.Button({
			text : '修改',
			iconCls : 'icon-edit',
			handler : function() {
				if (!grid_orgdemocracy.checkOne()) {
					return;
				}
				window_edit_orgdemocracy.show(this);
				window_edit_orgdemocracy.center();
				var record = grid_orgdemocracy.getSelectionModel().getSelected();
				var frm = Ext.getCmp("form_edit_orgdemocracy").getForm();
				// set value
				frm.findField('orgDemocracy.id').setValue(record.get("id"));
				frm.findField('orgDemocracy.goalId').setValue(record.get("goalId"));
				frm.findField('orgDemocracy.orgId').setValue(record.get("orgId"));
				frm.findField('orgDemocracy.score').setValue(record.get("score"));
				frm.findField('orgDemocracy.memo').setValue(record.get("memo"));
				frm.findField('orgDemocracy.checkOrgId').setValue(record.get("checkOrgId"));
				frm.findField('orgDemocracy.year').setValue(record.get("year"));
				frm.findField('orgDemocracy.checkManId').setValue(record.get("checkManId"));
				frm.findField('orgDemocracy.scoreId').setValue(record.get("scoreId"));
			}
		});
var btn_del_orgdemocracy = new Ext.Button({
			text : '删除',
			iconCls : 'icon-del',
			handler : function() {
				var record = grid_orgdemocracy.getSelectionModel().getSelected();
				if (record) {
					Ext.Msg.confirm('确认删除', '你确定删除该条记录?', function(btn) {
								if (btn == 'yes') {
									Ext.Ajax.request({
												url : '/supervise/biz/orgdemocracy/deleteOrgDemocracy.action',
												params : {
													id : record.data.id
												},
												success : function() {
													grid_orgdemocracy.getStore().remove(record);
												},
												failure : function() {
													Ext.Msg.show({
																title : '错误提示',
																msg : '删除时发生错误!',
																buttons : Ext.Msg.OK,
																icon : Ext.Msg.ERROR
															});
												}
											});
								}
							});
				}
			}
		});
var btn_search_orgdemocracy = new Ext.Button({
	text : '查询',
	iconCls : 'icon-search',
	handler : function() {
		ds_orgdemocracy.load();
	}
});

var ds_orgdemocracy = new Ext.data.Store({
	proxy : new Ext.data.HttpProxy({
		url : '/supervise/biz/orgdemocracy/findAllOrgDemocracy.action'
	}),
	reader : new Ext.data.JsonReader({
		root : 'root',
		successProperty :'success'
	}, [{
	name : 'id',
	type : 'int'
}, {
	name : 'goalId',
	type : 'int'
}, {
	name : 'orgId',
	type : 'int'
},{
	name : 'score',
	type : 'float'
},{
	name : 'memo',
	type : 'string'
},{
	name : 'checkOrgId',
	type : 'int'
},{
	name : 'year',
	type : 'int'
},{
	name : 'checkManId',
	type : 'int'
},{
	name : 'auditOrgId',
	type : 'int'
},{
	name : 'scoreId',
	type : 'int'
}])
});

var grid_orgdemocracy = new Ext.grid.GridPanel({
			title : '社会评价指标考评',
			iconCls : 'icon-grid',
			loadMask : {
				msg : '数据加载中...'
			},
			viewConfig : {
				forceFit : true
			},
			region : 'center',
			cm : cm_orgdemocracy,
			sm : new Ext.grid.RowSelectionModel({
						singleSelect : true
					}),
			store : ds_orgdemocracy,
			autoExpandColumn : 'memo',
			tbar : [btn_add_orgdemocracy, '-', btn_edit_orgdemocracy, '-', btn_del_orgdemocracy, '-', btn_search_orgdemocracy],
			bbar : new Ext.PagingToolbar({
						pageSize : 20,
						store : ds_orgdemocracy,
						displayInfo : false,
						displayMsg : '第 {0} - {1} 条  共 {2} 条',
						refreshText : "刷新",
						emptyMsg : "没有记录"
					}),
			// 检测至少选择一个
			checkOne : function() {
				var selections = this.getSelectionModel().getSelections();
				if (selections.length == 0) {
					Ext.MessageBox.alert("提示", "请选择一条的记录！");
					return false;
				} else if (selections.length != 1) {
					Ext.MessageBox.alert("提示", "不能选择多行！");
					return false;
				}
				return true;
			},

			// 检测必须选择一个
			checkMany : function() {
				var selections = this.getSelectionModel().getSelections();
				if (selections.length == 0) {
					Ext.MessageBox.alert("提示", "请至少选择一条的记录！");
					return false;
				}
				return true;
			},
			listeners : {
				'render' : function(e) {
					ds_orgdemocracy.load();
				}
			}

		});

var p_orgdemocracy = {
	id : 'orgdemocracy-panel',
	border : false,
	layout : 'border',
	items : [grid_orgdemocracy]
};